<?php
	class Videos extends CI_Model{

	 	function __construct(){
        	parent::__construct();
    	}

		function get_video_cate(){
			$sql = "SELECT id, name from {$this->db->dbprefix}course_cate where school_id = $this->school_id and type=1";
			$result = $this->db->query($sql)->result_array();
			return $result;
		}

		function get_course_cate($cate_id){
			$sql = "SELECT id, name from {$this->db->dbprefix}course_cate_subject where pid =".$cate_id;
			$result = $this->db->query($sql)->result_array();
			return $result;
		}


		function get_course_info($id){
			$sql = "SELECT c.*,ot.teacher_name AS school_teacher_name, cs.fx_price,cs.is_free,ot.school_name,ot.header_img,ot.intro,os.xiaozhang,os.link_phone,os.school_logo,os.id as sid from {$this->db->dbprefix}course as c left join {$this->db->dbprefix}o2o_teachers as ot on(c.teacher_id = ot.id) left join {$this->db->dbprefix}o2o_school as os on(os.id = $this->school_id) left join {$this->db->dbprefix}course_shelves as cs on(cs.school_id=$this->school_id and cs.course_id = c.id) where c.id = ".$id;
			$result = $this->db->query($sql)->row_array();
			return $result;
		}

		function get_live_course_info($id){
			$sql = "SELECT c.*,ot.teacher_name,ot.school_name,ot.header_img,ot.intro,os.xiaozhang,os.link_phone,os.school_logo,os.id as sid from {$this->db->dbprefix}course as c left join {$this->db->dbprefix}o2o_teachers as ot on(c.teacher_id = ot.id) left join {$this->db->dbprefix}o2o_school as os on(ot.school_id = os.id) where c.school_id = $this->school_id and c.id = ".$id;
			$result = $this->db->query($sql)->row_array();
			return $result;
		}


		function get_course_chapter($course_id){
			$sql = "SELECT * from {$this->db->dbprefix}course_chapter where pid = 0 and course_id =".$course_id;
			$result = $this->db->query($sql)->result_array();
			foreach ($result as $key => $value) {
				$sql = "SELECT * from {$this->db->dbprefix}course_chapter where pid = ".$value['id']." ";
				$list = $this->db->query($sql)->result_array();
				$result[$key]['jie'] = $list;
			}
			return $result;
		}


		function get_video_info($course_id){
			$sql = "SELECT * from {$this->db->dbprefix}course_chapter where pid = 0 and course_id =".$course_id;
			$result = $this->db->query($sql)->result_array();
			foreach ($result as $key => $value) {
				$sql = "SELECT cc.*,v.image_url,v.video_url,v.duration from {$this->db->dbprefix}course_chapter as cc left join {$this->db->dbprefix}video as v on(cc.vid = v.id) where cc.pid = ".$value['id']." ";
				$list = $this->db->query($sql)->result_array();
				$result[$key]['jie'] = $list;
			}
			return $result;
		}


		function get_first_video_url($course_id,$chapter_id){
			if($chapter_id > 0){
				$sql = "SELECT cc.id as cid,cc.course_id,v.image_url,v.video_url from {$this->db->dbprefix}course_chapter as cc left join {$this->db->dbprefix}video as v on(cc.vid = v.id) where cc.id = $chapter_id and v.status = 1 and cc.course_id=".$course_id."  order by cc.id asc limit 0,1 ";
			}else{
				$sql = "SELECT cc.id as cid,cc.course_id,v.image_url,v.video_url from {$this->db->dbprefix}course_chapter as cc left join {$this->db->dbprefix}video as v on(cc.vid = v.id) where cc.vid > 0 and v.video_url is not null and v.status = 1 and cc.course_id=".$course_id." order by cc.id asc limit 0,1 ";
			}

			$result = $this->db->query($sql)->row_array();
			return $result;
		}


		function get_school_course_count($sid = 0){
			$sql = "SELECT count(*) as count from {$this->db->dbprefix}course where school_id = $sid";
			$result = $this->db->query($sql)->row_array();
			return $result['count'];
		}


		function get_school_xuesheng_count($sid = 0){
			$sql = "SELECT count(*) as count from {$this->db->dbprefix}o2o_school_student where school_id = $sid";
			$result = $this->db->query($sql)->row_array();
			return $result['count'];
		}


		function get_comment_list($course_id,$limit,$offset){
			$where = "where cc.top_id = ".$course_id."";
			$sql = "SELECT cc.content,cc.add_time,u.head_image_url,u.nick from {$this->db->dbprefix}course_comment as cc left join {$this->db->dbprefix}user as u on(cc.uid = u.id) $where order by cc.id desc limit $limit,$offset";
			$result = $this->db->query($sql)->result_array();

			$sql = "SELECT count(cc.id) as count from {$this->db->dbprefix}course_comment as cc left join {$this->db->dbprefix}user as u on(cc.uid = u.id) $where";
			$count = $this->db->query($sql)->row_array();
			return array("result"=>$result,"count"=>$count['count']);

		}
	}
